import Mock from "mockjs";

//获取请求参数
function getParams(options) {
  const paramsStr = /.+\?(.+)$/.exec(options.url)[1]; // 将 ? 后面的字符串取出来
  const paramsArr = paramsStr.split("&"); // 将字符串以 & 分割后存到数组中
  let paramsObj = {};
  // 将 params 存到对象中
  paramsArr.forEach((param) => {
    if (/=/.test(param)) {
      // 处理有 value 的参数
      let [key, val] = param.split("="); // 分割 key 和 value
      val = decodeURIComponent(val); // 解码
      val = /^\d+$/.test(val) ? parseFloat(val) : val; // 判断是否转为数字
      if (paramsObj.hasOwnProperty(key)) {
        // 如果对象有 key，则添加一个值
        paramsObj[key] = [].concat(paramsObj[key], val);
      } else {
        // 如果对象没有这个 key，创建 key 并设置值
        paramsObj[key] = val;
      }
    } else {
      // 处理没有 value 的参数
      paramsObj[param] = true;
    }
  });

  return paramsObj;
}

//数据返回延迟时间
Mock.setup({
  timeout: "300-800",
});

//获取商品信息
Mock.mock("/home/goodslist", "get", {
  code: 0,
  msg: "请求成功",
  data: [
    {
      name: "今日支付订单",
      "value|30-100": 10,
    },
    {
      name: "今日收藏订单",
      "value|10-100": 10,
    },
    {
      name: "今日未支付订单",
      "value|10-100": 10,
    },
    {
      name: "本月支付订单",
      "value|500-2000": 10,
    },
    {
      name: "本月收藏订单",
      "value|100-900": 10,
    },
    {
      name: "本月未支付订单",
      "value|100-500": 10,
    },
  ],
});

//获取商品图表数据
let list = [];
for (let i = 0; i < 7; i++) {
  list.push(
    Mock.mock({
      苹果: Mock.Random.float(100, 8000, 0, 0),
      OPPO: Mock.Random.float(100, 8000, 0, 0),
      vivo: Mock.Random.float(100, 8000, 0, 0),
      小米: Mock.Random.float(100, 8000, 0, 0),
      华为: Mock.Random.float(100, 8000, 0, 0),
      三星: Mock.Random.float(100, 8000, 0, 0),
    })
  );
}
let goodslistData = Mock.mock({
  //饼状图
  pieData: [
    {
      name: "苹果",
      "value|1000-6000": 1000,
    },
    {
      name: "OPPO",
      "value|1000-6000": 1000,
    },
    {
      name: "vivo",
      "value|1000-6000": 1000,
    },
    {
      name: "小米",
      "value|1000-6000": 1000,
    },
    {
      name: "华为",
      "value|1000-6000": 1000,
    },
    {
      name: "三星",
      "value|1000-6000": 1000,
    },
  ],
  //柱状图
  barData: [
    {
      date: "周一",
      "new|5-100": 5,
      "active|200-800": 200,
    },
    {
      date: "周二",
      "new|5-100": 5,
      "active|200-800": 200,
    },
    {
      date: "周三",
      "new|5-100": 5,
      "active|200-800": 200,
    },
    {
      date: "周四",
      "new|5-100": 5,
      "active|200-800": 200,
    },
    {
      date: "周五",
      "new|5-100": 5,
      "active|200-800": 200,
    },
    {
      date: "周六",
      "new|5-100": 5,
      "active|200-800": 200,
    },
    {
      date: "周日",
      "new|5-100": 5,
      "active|200-800": 200,
    },
  ],
  //折线图
  lineData: {
    date: [
      "20230301",
      "20230302",
      "20230303",
      "20230304",
      "20230305",
      "20230306",
      "20230307",
    ],
    data: list,
  },
  //tatble数据
  tableData: {
    data: [
      {
        name: "苹果",
        "today|10-200": 22,
        "month|100-1000": 100,
        "all|2000-5000": 500,
      },
      {
        name: "OPPO",
        "today|10-200": 22,
        "month|100-1000": 100,
        "all|2000-5000": 500,
      },
      {
        name: "viov",
        "today|10-200": 22,
        "month|100-1000": 100,
        "all|2000-5000": 500,
      },
      {
        name: "小米",
        "today|10-200": 22,
        "month|100-1000": 100,
        "all|2000-5000": 500,
      },
      {
        name: "华为",
        "today|10-200": 22,
        "month|100-1000": 100,
        "all|2000-5000": 500,
      },
      {
        name: "三星",
        "today|10-200": 22,
        "month|100-1000": 100,
        "all|2000-5000": 500,
      },
    ],
  },
});
Mock.mock("/home/datalist", "get", {
  code: 0,
  msg: "获取成功",
  data: goodslistData,
});

//用户列表
const userDatas = Mock.mock({
  "datas|320": [
    {
      "id|+1": 1,
      name: "@cname",
      sex: '@character("ABC")',
      birthday: "@date()",
      address: "@county(true)",
    },
  ],
});

//分页获取用户信息列表
Mock.mock(RegExp("user/userlist" + ".*"), "get", (options) => {
  const paramsObj = getParams(options);

  //三个参数 page , limit , count
  const arr = [...userDatas.datas]; //用户的所有数据
  const total = arr.length; //数据总数
  let sum = paramsObj.page * paramsObj.limit; //需要获取的数据个数
  if (sum >= total) {
    sum = total;
  }
  const list = arr.slice(paramsObj.count, sum); //需要数据
  return Mock.mock({
    code: 0,
    msg: "获取成功",
    data: {
      list,
      total,
    },
  });
});

//修改用户信息
Mock.mock("user/userlist", "post", (options) => {
  let user = JSON.parse(options.body);
  userDatas.datas.forEach((item, i) => {
    if (item.id === user.id) {
      userDatas.datas[i] = user;
    }
  });

  return Mock.mock({
    code: 0,
    msg: "修改成功",
    data: true,
  });
});

//删除指定用户
Mock.mock(RegExp("user/userlist" + ".*"), "delete", (options) => {
  const { id } = getParams(options);

  const data = userDatas.datas.filter((item) => {
    if (item.id === id) {
      const i = userDatas.datas.indexOf(item);
      userDatas.datas.splice(i, 1);
      return item;
    }
  });
  return Mock.mock({
    code: 0,
    msg: "删除成功",
    data,
  });
});

//搜索用户(模糊搜索)
Mock.mock(RegExp("user/searchuser" + ".*"), "get", (options) => {
  const { type } = getParams(options);
  let data = userDatas.datas.filter((item) => {
    return (item.name + "").includes(type) || (item.id + "").includes(type);
  });
  if (!data.length) {
    data = null;
  }

  return {
    code: 0,
    msg: "搜索成功",
    data,
  };
});

//添加用户
Mock.mock("user/adduser", "post", (options) => {
  let user = JSON.parse(options.body);
  const endUserId = userDatas.datas.at(-1).id;
  user.id = endUserId + 1;
  userDatas.datas.push(user);
  return Mock.mock({
    code: 0,
    msg: "添加成功",
    data: userDatas.datas.length,
  });
});

//商品数据
const mall = Mock.mock({
  data: [
    {
      id: 1,
      name: "苹果",
      off: false,
      discount: 8899,
      price: 8999,
      src: "@image('500x300', '#fff','#000', 'iPhone')",
    },
    {
      id: 2,
      name: "OPPO",
      off: false,
      discount: 3199,
      price: 3319,
      src: "@image('500x300', '#007200','#fff', 'OPPO')",
    },
    {
      id: 3,
      name: "viov",
      off: false,
      price: 3399,
      discount: "",
      src: "@image('500x300', '#4e6ef2','#fff', 'vivo')",
    },
    {
      id: 4,
      name: "小米",
      off: false,
      discount: 2999,
      price: 3999,
      src: "@image('500x300', '#ff7939', 'Mi')",
    },
    {
      id: 5,
      name: "华为",
      off: false,
      discount: 5999,
      price: 6100,
      src: "@image('500x300', '#fff','#000', 'HUAWEI')",
    },
    {
      id: 6,
      name: "三星",
      off: false,
      discount: 8499,
      price: 8599,
      src: "@image('500x300', '#fff','#4e6ef2', 'SAMSUNG')",
    },
  ],
});

//获取商品数据
Mock.mock("mall/mallList", "get", {
  code: 0,
  msg: "获取成功",
  data: mall.data,
});

//修改商品上下架
Mock.mock("mall/setoff", "post", (options) => {
  let { id, off } = JSON.parse(options.body);
  mall.data.forEach((item) => {
    if (item.id === id) {
      item.off = off;
    }
  });
  return Mock.mock({
    code: 0,
    msg: "设置成功",
    data: true,
  });
});

//修改商品信息
Mock.mock("mall/update", "put", (options) => {
  const updateForm = JSON.parse(options.body);
  mall.data.forEach((item, i) => {
    if (item.id === updateForm.id) {
      mall.data[i] = updateForm;
    }
  });
  return Mock.mock({
    code: 0,
    msg: "修改成功",
    data: updateForm.id,
  });
});

let oneMallKEY = 7;
//添加商品
Mock.mock("mall/post", "post", (options) => {
  const {
    name,
    price,
    discount = "",
    imgageName,
    off = false,
  } = JSON.parse(options.body);
  //添加商品详情数据
  const id = oneMallKEY++;
  const data = Mock.mock({
    id,
    name: name,
    off,
    discount,
    price,
    src: Mock.Random.image("500x300", "#fff", "#000", imgageName),
  });
  mall.data.push(data);
  //添加首页商品数据
  //折线图
  goodslistData.lineData.data.forEach((item, i, my) => {
    item[name] = Mock.Random.float(80, 8000, 0, 0);
  });

  //饼状图
  goodslistData.pieData.push(
    Mock.mock({
      name,
      "value|1000-6000": 1000,
    })
  );
  //tabal数据
  goodslistData.tableData.data.push(
    Mock.mock({
      name,
      "today|10-200": 22,
      "month|100-1000": 100,
      "all|2000-5000": 500,
    })
  );
  return Mock.mock({
    code: 0,
    msg: "",
    data: null,
  });
});
